package org.agilelang.generator.cakephp;

/*
 AgileLang - A fourth generation language(http://www.agilelang.org).

 Copyright (C) 2013 Roderick Cusirramos (roderick20@hotmail.com)

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.util.List;
import org.agilelang.mvc.Facade;
import org.agilelang.object.Controller;

public class ICakePHP extends Facade {

    String tpl = "";
    public void setTemplate(String clasic) {
        tpl = clasic;
    }  
    
    public ICakePHP(String path_xml, String path_app){
        this.path_xml = path_xml;
        this.path_app = path_app;
        //scanner_parser(new JTextArea());
        readProject();
    }
            
public void processApp() {
        processFile();
        processMenu();
        processController();
        processModel();  
        processReport();
    }
    public void processFile() {
        GFile gfile = new GFile(path_app, this.project.application);
        gfile.setTemplate(tpl);
        gfile.process();
        //gfile.otherFile(project.application.name);
        //gfile.generateDatabase_php(project.application.host, project.application.login, project.application.password, project.application.database);
    }

    

    public void processMenu() {
        GMenu generate_menu = new GMenu(project.application.name, path_app + "/app/", project.menus);
        generate_menu.setTemplate(tpl);
        generate_menu.process();
    }

    public void processController() {
        List<Controller> controllers = project.controllers;
        int t = controllers.size();
        for (int i = 0; i < t; i++) {
            Controller controller = controllers.get(i);
            
            if (controller.type.equals("simple")) {
                GControllerSimple generate_controller = new GControllerSimple(path_app + "/app/", project, controller);
                generate_controller.setTemplate(tpl);
                generate_controller.process();
            } else if (controller.type.equals("scaffold")) {
                GControllerScaffold generate_controller = new GControllerScaffold(path_app + "/app/", project, controller);
                generate_controller.setTemplate(tpl);
                generate_controller.process();
            }else if (controller.type.equals("scaffold_extend")) {
                GControllerScaffoldExtend generate_controller = new GControllerScaffoldExtend(path_app + "/app/", project, controller);
                generate_controller.setTemplate(tpl);
                generate_controller.process();
            } 
            
            else if (controller.type.equals("transaction")) {
                GControllerTransaction generate_controller = new GControllerTransaction(path_app + "/app/", project,controller);
                generate_controller.setTemplate(tpl); 
                generate_controller.process();
            } else {
                //error
            }
        }
    }

    public void processModel() {
        GModel generate_model = new GModel(path_app + "/app/", project.models);
        generate_model.setTemplate(tpl);
        generate_model.process();
    }   

    public void processReport() {
        GReport generate_menu = new GReport(path_app + "/app/", project,  project.reports);
        generate_menu.setTemplate(tpl);
        generate_menu.process();
    }
}
